<?php
// 统一成功返回
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\Config;
use think\facade\Db;

function success($msg = '', $data = null)
{
    return json([
        'code' => 1,
        'msg' => $msg,
        'data' => $data
    ]);
}

// 统一失败返回
function error($msg = '', $data = null)
{
    return json([
        'code' => 0,
        'msg' => $msg,
        'data' => $data
    ]);
}

//签发token
function sendToken($user_id,$mobile)
{
    $data = [
        'iss'=>request()->domain(),
        'itm'=>time(),
        'data'=>[
            'user_id'=>$user_id,
            'mobile'=>$mobile
        ]
    ];
    $key = config('app.Jwt_key');

    $token =  \Firebase\JWT\JWT::encode($data,$key,'HS256');
    return $token;
}

function add_money($user_id, $money, $reason, $type = 1)
{
    // 获取用户信息
    $user = Db::name('user_information')->where('id', $user_id)->find();
    if (!$user) {
        throw new Exception('用户不存在');
    }

    // 计算新的余额
    $newBalance = $user['wallet'] + $money;

    // 更新用户余额
    Db::name('user_information')->where('id', $user_id)->update(['wallet' => $newBalance]);
    // 记录流水
    Db::name('wallet')->insert([
        'user_id' => $user_id,
        'money' => $money,
        'reason' => $reason,
        'before' => $user['wallet'],
        'after' => $newBalance,
        'type' => $type, // 类型：1-存储
        'create_time' => date('Y-m-d H:i:s')
    ]);

    return $newBalance;
}

//测试验证码
function sendCode($mobile)
{
    $code = mt_rand(1111, 9999);
    Db::name('code')->insert([
        'mobile' => $mobile,
        'code' => $code,
        'send_time' => time()
    ]);
    echo $code;
}



function money_log($user_id,$wallet,$before,$after,$type){
    $money_log = [
        'user_id'=>$user_id,
        'wallet'=>$wallet,
        'before'=>$before,
        'after'=>$after,
        'type'=>$type,
        'create_time'=>date('Y:m:d H:i:s')
    ];
    $res = \api\project\model\MoneyModel::create($money_log);
    if($res)
    {
        return json(['code'=>'1','msg'=>'流水记录表添加成功']);
    }else{
        return json(['code'=>'0','msg'=>'流水记录表添加失败']);
    }
}




//阿里云验证码
function sendAliSms($mobile)
{
    $config = new Config([
        // 从阿里云获取
        'accessKeyId' => config('app.aliSms.accessKeyId'),
        'accessKeySecret' => config('app.aliSms.accessKeySecret'),
    ]);

    $config->endpoint = "dysmsapi.aliyuncs.com";
    $client = new Dysmsapi($config);

    // 运行时参数超时设置，仅对使用了该运行时参数实例的请求有效
    $runtimeOptions = new RuntimeOptions();
    // true 忽略证书校验
    $runtimeOptions->ignoreSSL = true;

    $sendSmsRequest = new SendSmsRequest();
    // 手机号
    $sendSmsRequest->phoneNumbers = $mobile;
    // 签名名称
    $sendSmsRequest->signName = '替换为你申请的签名';
    // 模板CODE
    $sendSmsRequest->templateCode = '替换为你申请的模板CODE';
    // 生成随机数作为验证码
    $code = mt_rand(1111, 9999);
    $sendSmsRequest->templateParam = json_encode(['code' => $code]);
    try {
        $response = $client->sendSmsWithOptions($sendSmsRequest, $runtimeOptions);
    } catch (\Exception $exception) {
        halt($exception->getMessage());
    }
    if ($response->body->code == 'OK') {
        Db::name('sms_code')->insert([
            'mobile' => $mobile,
            'code' => $code,
            'send_time' => time()
        ]);
        echo '短信发送成功！';
    }
}

